class Byte_8bit:
    def __init__(self, x=0):
        self.val = x & 0xff

    def update(self):
        self.val = self.val & 0xff

    def __get__(self):
        self.update()
        return self.val

    def __set__(self, value):
        if value > 0xFF:
            print("#ERROR# < 8 bit : set : overwrite>")
            return
        self.val = value
        self.update()

    def __setitem__(self, index, value):            #以索引的方式对8bit按位赋值
        if index > 7 or index < 0:
            print("#ERROR# < 8 bit : setitem : index out of range>")
            return
        if value != 0 and value != 1:
            print("#ERROR# < 8 bit : setitem : index {} must be 0 or 1>".format(index))
            return
        if value:
            self.val = self.val | (1 << index)
        else:
            self.val = self.val & ~(1 << index)
        self.update()

    def __getitem__(self, index):                   #以索引的方式对8bit按位取值
        self.update()
        if self.val & (1 << index):
            return 1
        else:
            return 0
    
    def __str__(self):
        return "<Byte>\t\t{:08b}".format(self.val)


class Half_Word_16bit:
    def __init__(self, x=0):
        self.val = x & 0xffff

    def update(self):
        self.val = self.val & 0xffff

    def __get__(self):
        self.update()
        return self.val

    def __set__(self, value):
        if value > 0xFFFF:
            print("#ERROR# < 16 bit : set : overwrite>")
            return
        self.val = value
        self.update()

    def __setitem__(self, index, value):            #以索引的方式对16bit按位赋值
        if index > 15 or index < 0:
            print("#ERROR# < 16 bit : setitem : index out of range>")
            return
        if value != 0 and value != 1:
            print("#ERROR# < 16 bit : setitem : index {} must be 0 or 1>".format(index))
            return
        if value:
            self.val = self.val | (1 << index)
        else:
            self.val = self.val & ~(1 << index)
        self.update()

    def __getitem__(self, index):                   #以索引的方式对16bit按位取值
        self.update()
        if self.val & (1 << index):
            return 1
        else:
            return 0
    
    def __str__(self):
        return "<Half_Word>\t{:016b}".format(self.val)



class Word_32bit:
    def __init__(self, x=0):
        self.val = x & 0xffffffff

    def update(self):
        self.val = self.val & 0xffffffff

    def __get__(self):
        self.update()
        return self.val

    def __set__(self, value):
        if value > 0xFFFFFFFF:
            print("#ERROR# < 32 bit : set : overwrite>")
            return
        self.val = value
        self.update()

    def __setitem__(self, index, value):            #以索引的方式对32bit按位赋值
        if index > 31 or index < 0:
            print("#ERROR# < 32 bit : setitem : index out of range>")
            return
        if value != 0 and value != 1:
            print("#ERROR# < 32 bit : setitem : index {} must be 0 or 1>".format(index))
            return
        if value:
            self.val = self.val | (1 << index)
        else:
            self.val = self.val & ~(1 << index)
        self.update()

    def __getitem__(self, index):                   #以索引的方式对32bit按位取值
        self.update()
        if self.val & (1 << index):
            return 1
        else:
            return 0
    
    def __str__(self):
        return "<Word>\t\t{:032b}".format(self.val)

def data_slice(data,left,right):
    len = left - right + 1
    return (data>>right)%(1<<len)

def sign_expend(x, len):                # 无符号类型 转为 有符号类型
    if x & (1 << len - 1):
        return x - (1 << len)
    else:
        return x

# a = Half_Word_16bit(0b10101)
# a[0] = 1
# print(a)
# print(bin(data_slice(a.val,4,0)))

# b = Word_32bit(0b111)
# b[0] = 0
# print(b)

# c = Byte_8bit(0b111)
# c[0] = 0
# print(c)
